package com.chlp.core.framework.codeGenerator;

import com.chlp.core.framework.codeGenerator.entity.BasisInfo;
import com.chlp.core.framework.codeGenerator.util.EntityInfoUtil;
import com.chlp.core.framework.codeGenerator.util.Generator;
import com.chlp.core.framework.codeGenerator.util.MySqlToJavaUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈礼鹏
 * @ Package: com.github.mybatis.fl.convert
 * @ date: 2019年4月9日 下午8:15:25
 */
public class CodeGeneratorMain {
    /**
     * 基础信息
     */
    private static final String PROJECT = "NIO_CQA";
    private static final String AUTHOR = "LiPeng.Chen";
    private static final String VERSION = "V1.0";
    /**
     * 类信息
     */
    private static final String TIME = "2020年08月13日";
    private static final String AGILE = System.currentTimeMillis() + "";
    /**
     * 数据库连接信息【需要根据修改】
     */
    private static final String URL = "jdbc:mysql://localhost:3306/spring_boot?allowMultiQueries=true&noAccessToProcedureBodies=true&characterEncoding=utf-8&serverTimezone=UTC";
    private static final String NAME = "root";
    private static final String PASS = "CHLPchlp123.";
    private static final String DATABASE = "spring_boot";
    /**
     * 项目名称以及项目路径配置【需要根据修改】
     * 生成文件存放位置:service模块的地址
     * 生成文件存放位置:common模块的地址
     */
    private static final String PROJECT_PATH = "C:\\Users\\chlp\\Desktop\\";
    private static final String PROJECT_COMMON_NAME = "chlp-common";
    private static final String PROJECT_SERVICE_NAME = "chlp-service";
    private static final String SERVICE_PATH = PROJECT_PATH + PROJECT_SERVICE_NAME + "\\src\\main\\java\\";
    private static final String ENTITY_PATH = PROJECT_PATH + PROJECT_COMMON_NAME + "\\src\\main\\java\\";
    /**
     * 路径信息，分开路径方便聚合工程项目，微服务项目【需要根据修改】
     */
    private static final String PRE_PACKAGE = "com.efast.quality";
    private static final String COMMON_PACKAGE = PRE_PACKAGE + "." + "utils.common";
    private static final String ENTITY_PACKAGE = PRE_PACKAGE + "." + "entity";
    private static final String BEAN_PACKAGE = PRE_PACKAGE + "." + "bean";
    private static final String DAO_PACKAGE = PRE_PACKAGE + "." + "dao";
    private static final String MAPPER_PACKAGE = PRE_PACKAGE + "." + "dao.mapper";
    private static final String SERVICE_PACKAGE = PRE_PACKAGE + "." + "service";
    private static final String SERVICE_IMPL_PACKAGE = PRE_PACKAGE + "." + "service.impl";
    private static final String CONTROLLER_PACKAGE = PRE_PACKAGE + "." + "controller";

    public static void main(String[] args) {

        // 定义需要生成那些table
        List<String> tables = new ArrayList<>();
        tables.add("student");

        // 生成那些文件：实体类，分页bean，dao，xml,service，impl,controller层
        List<String> types = new ArrayList<>();
        types.add(ENTITY_PACKAGE);
        types.add(BEAN_PACKAGE);
        types.add(DAO_PACKAGE);
        types.add(MAPPER_PACKAGE);
        types.add(SERVICE_PACKAGE);
        types.add(SERVICE_IMPL_PACKAGE);
        types.add(CONTROLLER_PACKAGE);

        CodeGeneratorMain.genFiles(tables, types);
    }

    private static void genFiles(List<String> tables, List<String> types) {
        tables.forEach(v -> {
            BasisInfo bi = new BasisInfo(PROJECT, AUTHOR, VERSION, URL, NAME, PASS, DATABASE, TIME, AGILE, ENTITY_PACKAGE, DAO_PACKAGE,
                    MAPPER_PACKAGE, SERVICE_PACKAGE, SERVICE_IMPL_PACKAGE, CONTROLLER_PACKAGE, COMMON_PACKAGE, BEAN_PACKAGE);
            bi.setTable(v);
            bi.setEntityName(MySqlToJavaUtil.getClassName(v));
            bi.setObjectName(MySqlToJavaUtil.changeToJavaFiled(v));
            bi.setEntityComment(null);
            EntityInfoUtil.getInfo(bi);

            //以下是生成： entity pageBean
            if (types.contains(ENTITY_PACKAGE)) {
                System.out.println(Generator.createEntity(ENTITY_PATH, bi).getMessage());
            }
            if (types.contains(BEAN_PACKAGE)) {
                System.out.println(Generator.createPageBean(ENTITY_PATH, bi).getMessage());
            }

            //以下是生成： dao xml service serviceImpl controller
            if (types.contains(DAO_PACKAGE)) {
                System.out.println(Generator.createDao(SERVICE_PATH, bi).getMessage());
            }
            if (types.contains(MAPPER_PACKAGE)) {
                System.out.println(Generator.createDaoImpl(SERVICE_PATH, bi).getMessage());
            }
            if (types.contains(SERVICE_PACKAGE)) {
                System.out.println(Generator.createService(SERVICE_PATH, bi).getMessage());
            }
            if (types.contains(SERVICE_IMPL_PACKAGE)) {
                System.out.println(Generator.createServiceImpl(SERVICE_PATH, bi).getMessage());
            }
            if (types.contains(CONTROLLER_PACKAGE)) {
                System.out.println(Generator.createController(SERVICE_PATH, bi).getMessage());
            }

            if (types.contains(COMMON_PACKAGE)) {
                System.out.println(Generator.createCrudUtils(SERVICE_PATH, bi));
                System.out.println(Generator.createCommonMapper(SERVICE_PATH, bi).getMessage());
                System.out.println(Generator.createCommonDao(SERVICE_PATH, bi).getMessage());
            }

            // 创建swagger配置 swagger配置文件还未上传至maven中央库，代码中已实现。
//			System.out.println(Generator.createSwaggerConfig(SERVICE_PATH, bi).getMessage());
        });
    }
}
